import { retina } from './browser'

// 雪碧图
import spriteJson from '../../../../public/resources/mapbox/sprite.json'
import sprite from '../../../../public/resources/mapbox/sprite.png'
import spriteJson2x from '../../../../public/resources/mapbox/sprite@2x.json'
import sprite2x from '../../../../public/resources/mapbox/sprite@2x.png'

export function getSprite() {
  if (retina()) {
    return {
      sprite: sprite2x,
      spriteJson: spriteJson2x
    }
  } else {
    return {
      sprite: sprite,
      spriteJson: spriteJson
    }
  }
}

export function getSpriteIcon(name) {
  const { sprite, spriteJson } = getSprite()
  for (let i in spriteJson) {
    if (i === name) {
      const info = spriteJson[i]
      const icon = Object.assign({}, info)
      icon.icon = sprite

      return icon
    }
  }
}
// 暂时无用
export function clipSprite(sprite, spriteInfo) {
  const offscreen = new OffscreenCanvas(spriteInfo.width, spriteInfo.height)
  const ctx = offscreen.getContext('2d')

  ctx.drawImage(sprite, 0, 0, spriteInfo.width, spriteInfo.height, spriteInfo.x, spriteInfo.y, spriteInfo.width, spriteInfo.height)

  canvas.convertToBlob().then((blob) => {
    uploadImgBlod(blob, props.resultId, props.resultType, process.env.VUE_APP_SYSTEM_MODULE_ASS, handleUploadImgBlod)
    const file = new FileReader()
    file.readAsDataURL(obj)
    file.onload = function(e) {
      console.log(e)
    }
  })
}

